/*
 * Copyright (c) 2020-2021. the original authors and DEPSEA.ORG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.depsea.oss.client.aliyun;

import com.aliyun.oss.OSSClient;
import org.depsea.oss.client.CustomerBucketOssTemplate;
import org.depsea.oss.client.OssType;
import org.depsea.oss.filter.FileUploadFilter;
import org.depsea.oss.model.FileInfo;
import org.depsea.oss.model.FileStreamInfo;
import org.depsea.oss.strategy.FilenameHandleStrategy;
import org.depsea.oss.strategy.OriginalFilenameHandleStrategyImpl;
import org.springframework.util.CollectionUtils;

import jakarta.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Aliyun OSS0
 *
 * @author Wang Chengwei
 * @since 1.0.0 2021/12/2 16:40
 */
public class AliyunCustomerBucketOssTemplate extends AbstractAliyunOssTemplate implements CustomerBucketOssTemplate {

    private FilenameHandleStrategy filenameHandleStrategy = new OriginalFilenameHandleStrategyImpl();

    private final List<FileUploadFilter> filters = new ArrayList<>();

    public AliyunCustomerBucketOssTemplate(OSSClient ossClient) {
        super(ossClient);
    }

    public void setFilenameHandleStrategy(FilenameHandleStrategy filenameHandleStrategy) {
        this.filenameHandleStrategy = filenameHandleStrategy;
    }

    public void addFilters(List<FileUploadFilter> filters) {
        if (!CollectionUtils.isEmpty(filters)) {
            this.filters.addAll(filters);
        }
    }

    public void addFilters(FileUploadFilter filter) {
        this.filters.add(filter);
    }

    @Override
    protected List<FileUploadFilter> getFilters() {
        return this.filters;
    }

    @Override
    protected String getOssTypeName() {
        return OssType.ALIYUN.name();
    }

    @Override
    public FileInfo upload(String bucketName, String folder, String filename, String originalFilename, String contentType, InputStream ins) {
        return this.doUpload(bucketName, folder, filename, originalFilename, contentType, ins);
    }

    @Override
    public FileStreamInfo get(String bucketName, String fullPath) {
        return this.doGet(bucketName, fullPath);
    }

    @Override
    public void download(String bucketName, String fullPath, HttpServletResponse response) {
        this.doDownload(bucketName, fullPath, response);
    }

    @Override
    public void download(String bucketName, String fullPath, String filename, HttpServletResponse response) {
        this.doDownload(bucketName, fullPath, filename, response);
    }

    @Override
    public void preview(String bucketName, String fullPath, HttpServletResponse response) {
        this.doPreview(bucketName, fullPath, response);
    }

    @Override
    public FilenameHandleStrategy getFilenameHandleStrategy() {
        return this.filenameHandleStrategy;
    }

    @Override
    public void delete(String bucketName, String ossKey) {
        this.deleteObject(bucketName, ossKey);
    }
}
